package net.elive.common.p2pnet.message;

import java.net.DatagramPacket;

import net.elive.common.p2pnet.AbstractMessage;
import net.elive.tools.ByteLongSwitch;
import net.elive.tools.ByteShortSwitch;
import net.elive.tools.MessageType;
/**
 * 时间同步返回信息数据包
 * @author jincool.cao@gmail.com 
 *  
 */
public class TimeSynMessageBack extends AbstractMessage {

	private short sType = MessageType.STYPE_TIME_SYN_MESSAGE_BACK;
	private byte[] dpData =  new byte[12];
	private long nowTime;
	/**
	 * 构造方法
	 * 时间同步消息数据包(共4个字节)
	 * 版本号   2B
	 * 消息服务类型2B  
	 * 系统当前时间8B
	 */
	public TimeSynMessageBack(){
		byte[] dpData  = new byte[4];
		/**
		 * 添加消息版本
		 */
		ByteShortSwitch.toByteArray(version, dpData, 0);
		/**
		 * 添加消息类型
		 */
		ByteShortSwitch.toByteArray(sType, dpData, 2);
		/**
		 * 添加系统当前时间
		 */
		ByteLongSwitch.toByteArray(System.currentTimeMillis(), dpData, 4);
		/**
		 * 数据包
		 */
		dp = new DatagramPacket(dpData,dpData.length);
	}
	
	public TimeSynMessageBack(DatagramPacket dp){
		this.dp = dp;
		dpData = dp.getData();
		version = ByteShortSwitch.toShort(dpData, 0);
		sType = ByteShortSwitch.toShort(dpData, 2);
		nowTime = ByteLongSwitch.toLong(dpData, 4);
	}

	public long getNowTime() {
		return nowTime;
	}

	
}
